#coding=utf-8
import unittest
from appium import webdriver
from selenium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from Tool.usefulTools import userfulToolsFactory
from selenium.common.exceptions import WebDriverException
from Public import BasePage
from selenium.webdriver.support.ui import WebDriverWait
from Public import BaseConstant
from Public import BasePage

navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
Newrecrport_Paramter_instances = BaseConstant.Newrecrport_Paramter
element_oprate_instance  = BasePage.element_Oprate()
element_find_instances = BasePage.BasicAction()

nav_name = "工作"
plugin_name = "问题上报"
description_text = "客户端自动化测试上报...."

class rec_report_factory():

    def open_Report_plug(self,driver,file_path):

        try:
            # 点击工作的导航，打开工作列表
            nav_plugin_factory.nav_open(navOpenFactory,nav_name.decode("utf-8"),driver)
            # 点击"问题上报"的插件，打开插件
            nav_plugin_factory.plugin_open(navOpenFactory,plugin_name.decode("utf-8"),driver)
            #上报插件打开之后进行截图
            element_oprate_instance.element_save_pic(file_path, 'open_Report_plug_successful')
        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'open_Report_plug_fail')
            raise

    def open_NewReportUI(self,driver,file_path):
        try:
            # 点击 新增按钮，打开新增界面
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.add_button_id, file_path)

            if element_oprate_instance.element_display_commit(Newrecrport_Paramter_instances.eventtype_list_id, file_path):
                element_oprate_instance.element_save_pic(file_path, 'open_NewReportUI_successful')

        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'open_NewReportUI_fail')
            raise
        print 'report plug open-'

    def chose_event_type(self,driver,file_path):
        # 选择问题类型、问题大小类
        try:
            # 点击"问题类型"的控件，打开下拉
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.eventtype_list_id, file_path)
            sleep(1)
            # 选择类别为：部件
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.eventtype_name, file_path)
            sleep(1)
            # 点击"大类"输入框，打开下拉列表
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.maintype_list_id, file_path)
            sleep(1)
            # 选择"大类"别为：市容环境
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.maintype_location, file_path)
            sleep(2)
            # 点击"小类"输入框，打开下拉列表
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.subtype_list_id, file_path)
            sleep(1)
            # 选择"小类"别为：果皮箱
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.subtype_location, file_path)
            element_oprate_instance.element_save_pic(file_path, 'chose_event_type_successful')

        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'chose_event_type_fail')
            raise

    def set_rec_desc(self,driver,file_path):

        try:
            #获取类内容作为上报的问题描述
            event_desc = element_find_instances.find_element(Newrecrport_Paramter_instances.subtype_name_location,file_path).get_attribute('text')
            # 输入“描述”内容
            element_oprate_instance.element_send_keys(Newrecrport_Paramter_instances.description_id, (event_desc+'--需处理--客户端自动化').decode("utf-8"), file_path)
            element_oprate_instance.element_save_pic(file_path, 'set_rec_desc_successful')
        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'set_rec_desc_fail')
            raise

    def set_Address_desc(self,driver,file_path):

        try:
            # 选择"上报界面的位置定位按钮"
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.location_report_id, file_path)
            sleep(2)
            # 点击"选定位置按钮"并确认
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.map_confirm_id, file_path)
            sleep(2)
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.ok_button_id, file_path)
            element_oprate_instance.element_save_pic(file_path, 'set_Address_desc_successful')

        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'set_Address_desc_fail')
            raise

    def choose_rec_pic(self,driver,file_path):

        try:
            # 从相册选取照片
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.media_view_id, file_path)
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.tool_photoadd_id, file_path)
            sleep(5)
            # 在相册点击确定按钮
            # EL = BasePage.Base.driver.find_element_by_xpath(Newrecrport_Paramter_instances.photo_xpath[2])
            # EL.click()
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.photo_xpath, file_path)
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.select_id, file_path)
            element_oprate_instance.element_save_pic(file_path, 'choose_rec_pic_successful')

        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'choose_rec_pic_fail')
            raise

    def new_rec_commit(self,driver,file_path):

        try:
            # 进行提交并确认
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.btn_report_id, file_path)
            #点击确认上报按钮
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.confirm_report_id, file_path)
            driver.implicitly_wait(15)
            # 获取上报结果内容
            ReportResult = element_find_instances.find_element(Newrecrport_Paramter_instances.report_result,file_path).get_attribute('text')
            # 判断上报结果，是否为上报成功
            if userfulToolsFactory.test_check(userfulTool, "test", ReportResult):
                print "上报成功...."
                element_oprate_instance.element_save_pic(file_path, 'new_rec_report_successful')
            else:
                print "上报失败...."
                element_oprate_instance.element_save_pic(file_path, 'new_rec_report_failed')
                raise
            # 点击上报完成之后的确认按钮
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.confirm_keys_name, file_path)

            element_oprate_instance.element_save_pic(file_path, 'new_rec_report')
            element_oprate_instance.element_click(Newrecrport_Paramter_instances.exit_id, file_path)

        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'choose_rec_pic_fail')
            raise

    def check_newRec_sucessful(self,driver,file_path):

        try:
            sleep(10)
            # 判断是否上报成功--根据内容进行判断
            # ReportResult = driver.find_element_by_xpath(
            #     "//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.ScrollView/android.widget.TextView").get_attribute(
            #     "text")
            ReportResult = element_find_instances.find_element(Newrecrport_Paramter_instances.report_result,file_path)
            ReportResult.get_attribute('text')
            if userfulToolsFactory.test_check(userfulTool, "test", ReportResult):
                print "report successful...."
                element_oprate_instance.element_save_pic(file_path, 'new_rec_report_successful')
            else:
                print "report failed...."
                element_oprate_instance.element_save_pic(file_path, 'new_rec_report_failed')
                raise
        except WebDriverException,error:
            print error
            element_oprate_instance.element_save_pic(file_path, 'new_rec_report_failed')
            raise


if __name__ == '__main__':
    unittest.main()
